<template>
  <div class="home">
    <div class="top">
      <p>运维计划</p>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>资产管理</el-breadcrumb-item>
        <el-breadcrumb-item>设备运维</el-breadcrumb-item>
        <el-breadcrumb-item>运维计划</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="header">
      <!-- 搜索 -->
      <el-input
        clearable
        style="width: 150px"
        v-model="name"
        placeholder="计划名称"
      ></el-input>
      <el-input
        clearable
        style="width: 150px"
        v-model="person"
        placeholder="责任人"
      ></el-input>
      <el-select
        clearable
        style="width: 150px"
        v-model="executionFrequency"
        placeholder="执行频率"
      >
        <!-- <el-option v-for="item in frequencyOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option> -->
        <el-option label="每月" value="每月"></el-option>
        <el-option label="每季度" value="每季度"></el-option>
      </el-select>
      <el-select clearable style="width: 150px" v-model="status" placeholder="状态">
        <el-option v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-date-picker clearable
        v-model="startTime"
        align="right"
        type="date"
        placeholder="开始日期"
      >
      </el-date-picker>
      <el-date-picker clearable
        v-model="endTime"
        align="right"
        type="date"
        placeholder="结束日期"
      >
      </el-date-picker>
      <el-button
        @click="onSearch"
        style="background-color: #0bb197"
        type="success"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button class="reset" @click="onReset" icon="el-icon-refresh"
        >重置</el-button
      >
    </div>

    <!-- 数据 -->
    <div class="body">
      <div class="div">
        <h4>查询结果列表</h4>
        <el-button
          style="background-color: #0bb197"
          type="success"
          @click="addDialog = true"
          >新增</el-button
        >
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column
          align="center"
          prop="id"
          label="序号"
          width="90"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="maintenancePlanNum"
          label="计划编号"
          width="140"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="maintenancePlanName"
          label="计划名称"
          width="150"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="maintenancePlanType"
          label="计划类型"
          width="120"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="person"
          label="责任人"
          width="120"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="startDay"
          label="开始时间"
          width="150"
        >
          <template slot-scope="scope"
            ><span>{{ timeTrans(scope.row.startDay, 1) }}</span></template
          >
        </el-table-column>
        <el-table-column
          align="center"
          prop="endDay"
          label="结束时间"
          width="150"
        >
          <template slot-scope="scope"
            ><span>{{ timeTrans(scope.row.endDay, 1) }}</span></template
          >
        </el-table-column>
        <el-table-column
          align="center"
          prop="executionFrequency"
          label="执行频率时间"
          width="150"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="remind"
          label="提前提醒(分钟)"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="maintenancePlanState"
          label="状态"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.maintenancePlanState == 0 ? "启用" : "停用"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="operation"
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              style="color: #0bb197"
              type="text"
              size="small"
              @click="openEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              style="color: #0bb197"
              type="text"
              @click="del(scope.row.id)"
              size="small"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.maintenancePlanState == 1"
              @click="state1(scope.row)"
              style="color: #0bb197"
              type="text"
              size="small"
              >启用</el-button
            >
            <el-button
              v-else
              style="color: #0bb197"
              type="text"
              size="small"
              @click="state(scope.row)"
              >停用</el-button
            >
            <!-- <el-button style="color: #0bb197" type="text" size="small" @click="state(maintenancePlanState = 0)">停用</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="count"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
        class="msg-pagination-container"
      >
      </el-pagination>
    </div>
    <!-- 新增数据 -->
    <el-dialog title="新增计划" :visible.sync="addDialog">
      <el-form :model="addData">
        <div class="addB">
          <el-form-item label="计划编号">
            <el-input
              v-model="addData.maintenancePlanNum"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="计划名称">
            <el-input
              v-model="addData.maintenancePlanName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </div>
        <div class="addB">
          <el-form-item label="计划类型">
            <el-select
              v-model="addData.maintenancePlanType"
              style="width: 350px"
            >
              <el-option label="维保1" value="维保1"></el-option>
              <el-option label="维保2" value="维保2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="责任人">
            <el-input v-model="addData.person" autocomplete="off"></el-input>
          </el-form-item>
        </div>
        <div class="addB">
          <el-form-item label="开始日期">
            <el-date-picker
              style="width: 350px"
              v-model="addData.startDay"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束日期">
            <el-date-picker
              style="width: 350px"
              v-model="addData.endDay"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </div>
        <div class="addB">
          <el-form-item label="执行频率">
            <el-select
              v-model="addData.executionFrequency"
              style="width: 350px"
            >
              <el-option label="每月" value="每月"></el-option>
              <el-option label="每季度" value="每季度"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="提醒时间（分钟）">
            <el-input v-model="addData.remind" autocomplete="off"></el-input>
          </el-form-item>
        </div>
        <div class="addB">
          <el-form-item label="执行时间">
            <el-date-picker
              style="width: 350px"
              v-model="addData.purchaseDate"
              type="date"
              placeholder="选择时段"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="选择设备">
            <el-select v-model="addData.choiceDevice" style="width: 350px">
              <el-option label="设备1" value="设备1"></el-option>
              <el-option label="设备2" value="设备2"></el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="button" @click="addDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="add"
          style="background-color: #0bb197; border: 1px solid #0bb197"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 修改数据 -->
    <el-dialog title="编辑计划" :visible.sync="editDialog">
      <el-form :model="editData">
        <div class="addB">
          <el-form-item label="计划编号">
            <el-input
              v-model="editData.maintenancePlanNum"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="计划名称">
            <el-input
              v-model="editData.maintenancePlanName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </div>
        <div class="addB">
          <el-form-item label="计划类型">
            <!-- <el-input v-model="addData.maintenancePlanType" autocomplete="off"></el-input> -->
            <el-select
              v-model="editData.maintenancePlanType"
              style="width: 350px"
            >
              <el-option label="维保1" value="维保1"></el-option>
              <el-option label="维保2" value="维保2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="责任人">
            <el-input v-model="editData.person" autocomplete="off"></el-input>
          </el-form-item>
        </div>
        <div class="addB">
          <el-form-item label="开始日期">
            <el-date-picker
              style="width: 350px"
              v-model="editData.startDay"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束日期">
            <el-date-picker
              style="width: 350px"
              v-model="editData.endDay"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </div>
        <div class="addB">
          <el-form-item label="执行频率">
            <el-select
              v-model="editData.executionFrequency"
              style="width: 350px"
            >
              <el-option label="每月" value="每月"></el-option>
              <el-option label="每季度" value="每季度"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="提醒时间（分钟）">
            <el-input v-model="editData.remind" autocomplete="off"></el-input>
          </el-form-item>
        </div>
        <div class="addB">
          <el-form-item label="执行时间">
            <el-date-picker
              style="width: 350px"
              v-model="editData.exeTime"
              type="date"
              placeholder="选择时段"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="选择设备">
            <!-- <el-input v-model="addData.choiceDevice" autocomplete="off"></el-input> -->
            <el-select v-model="editData.choiceDevice" style="width: 350px">
              <el-option label="设备1" value="设备1"></el-option>
              <el-option label="设备2" value="设备2"></el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="button" @click="editDialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="edit"
          style="background-color: #0bb197; border: 1px solid #0bb197"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import time from "../utils/time";
export default {
  mixins: [time],
  data: function () {
    return {
      status: "",
      person: "",
      executionFrequency: "",
      name: "",
      startTime: "",
      endTime: "",
      addDialog: false,
      editDialog: false,
      tableData: [],
      total: 1,
      count: 5,
      page: 1,
      addData: {
        choiceDevice: "",
        endDay: "",
        executionFrequency: "",
        id: 0,
        isDelete: 0,
        maintenancePlanName: "",
        maintenancePlanNum: "",
        maintenancePlanState: 0,
        maintenancePlanType: "",
        person: "",
        remind: 0,
        startDay: "",
      },
      editData: {},
      stateData: {},
      // frequencyOptions:[],
      // frequencyValue:'',
      statusOptions:[
        {
        value:0,
        label:'启用'
      },{
        value:1,
        label:'停用'
      }],
      statusValue:''
    };
  },
  methods: {
    //分页
    handleSizeChange(val) {
      this.count = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    },
    //数据渲染
    getData: function () {
      this.$axios({
        url: "/smart/maintenance-plan/list",
        method: "get",
        params: {
          pageNum: this.page,
          pageSize: this.count,
          status: this.status,
          person: this.person,
          frequency: this.executionFrequency,
          name: this.name,
          startTime: this.startTime,
          endTime: this.endTime,
        },
      }).then(({ data }) => {
        if (data.code == 2000) {
          this.tableData = data.data.list;
          this.total = data.data.total;
        } else {
          this.$message.error("数据加载失败！");
        }
      });
    },
    //封装执行频率
    // getSearch:function(){
    //   this.$axios({
    //     url: "/smart/maintenance-plan/list",
    //     method: "get",
    //     params: {
    //       pageNum: 1,
    //       pageSize: 100000,
    //     },
    //   }).then(({ data }) => {
    //     if (data.code == 2000) {
    //       this.tableData = data.data.list;
    //       console.log(555,this.tableData);
    //       data.data.list.forEach((v)=>{
    //         let item = {
    //           value:v.executionFrequency,
    //           label:v.executionFrequency
    //         }
    //         // for(var key in item){
    //         // }
    //         this.frequencyOptions.push(item)
    //       })
    //       this.total = data.data.total;
    //     }
    //   });
    // },
    //查询
    onSearch: function () {
      this.getData();
    },
    //重置
    onReset: function () {
      this.name = "",
      this.person = "",
      this.frequency = "",
      this.status = "",
      this.startTime = "",
      this.endTime = "";
      this.executionFrequency='',
      this.getData();
    },
    //新增数据
    add: function () {
      this.$axios({
        url: "/smart/maintenance-plan/add",
        method: "post",
        data: this.addData,
      }).then(({ data }) => {
        if ((data.code = 2000)) {
          console.log(data);
          this.$message.success("添加成功");
          this.addDialog = false;
          this.getData();
        } else {
          console.log(data);
          this.$message.error("添加失败");
        }
      });
    },
    //删除
    del: function (row) {
      this.$axios({
        url: "/smart/maintenance-plan/del/" + row,
        method: "DELETE",
      }).then(({ data }) => {
        console.log(data);
        if ((data.code = 2000)) {
          this.$message.success("删除成功");
          this.getData();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    //编辑计划
    openEdit: function (row) {
      //编辑框显示出来
      this.editDialog = true;
      //赋值
      let editData = { ...row };
      this.editData = editData;
    },
    edit: function () {
      this.$axios({
        url: "/smart/maintenance-plan/edit",
        method: "PUT",
        data: this.editData,
      }).then(({ data }) => {
        if (data.code == 2000) {
          this.$message.success("修改成功");
          this.editDialog = false;
          this.getData();
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //停用状态
    state: function (row) {
      // console.log(row);
      //赋值
      let stateData = { ...row };
      this.stateData = stateData;
      if ((stateData.maintenancePlanState = 1)) {
        this.stateData.maintenancePlanState = 1;
       
      } else {
        this.stateData.maintenancePlanState = 0;
      }
      console.log(this.stateData.maintenancePlanState);

      this.$axios({
        url: "/smart/maintenance-plan/edit",
        method: "PUT",
        data: this.stateData,
      }).then(({ data }) => {
        // console.log(data);
        if (data.code == 2000) {
          this.$message.success("成功");
          this.getData();
        } else {
          this.$message.error("失败");
        }
      });
    },
    //启用状态
     state1: function (row) {
      // console.log(row);
      //赋值
      let stateData = { ...row };
      this.stateData = stateData;
      if ((stateData.maintenancePlanState = 1)) {
        this.stateData.maintenancePlanState = 0;
       
      } else {
        this.stateData.maintenancePlanState = 1;
      }
      console.log(this.stateData.maintenancePlanState);

      this.$axios({
        url: "/smart/maintenance-plan/edit",
        method: "PUT",
        data: this.stateData,
      }).then(({ data }) => {
        // console.log(data);
        if (data.code == 2000) {
          this.$message.success("成功");
          this.getData();
        } else {
          this.$message.error("失败");
        }
      });
    },
  },
  mounted: function () {
    this.getData();
    // this.getSearch();
  },
};
</script>

<style scoped>
.home {
  height: 95%;
}
.top {
  display: flex;
  justify-content: space-between;
  margin: 20px 20px 10px 25px;
}
.header {
  height: 60px;
  background: white;
  margin: 10px 20px 20px;
  padding-top: 18px;
  padding-left: 15px;
  border-radius: 8px;
}
.reset {
  color: rgb(197, 196, 196);
}
.reset:hover {
  color: #0bb197;
}
.el-input,
.el-select {
  margin-right: 10px;
}
.el-button {
  margin-left: 10px;
}
.body {
  background: white;
  margin: 10px 20px;
  height: 84%;
  border-radius: 8px;
}
.el-pagination {
  margin: 25px 10px 0 0;
  display: flex;
  justify-content: right;
}
.div {
  display: flex;
  justify-content: space-between;
  padding: 20px;
}
.el-form-item {
  width: 350px;
}
.addB {
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.button:hover{
  color: #0bb197;
  border: 1px solid #0bb197;
}
</style>
<style>
.msg-pagination-container.is-background .el-pager li:not(.disabled).active {
  /*当前选中页数的样式进行修改*/
  background-color: #0bb197;
  color: #fff;
}
</style>
